package com.lry.basic.lambda;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo3 {

    public static void main(String[] args) {

        List<String> list  = Arrays.asList("ab","ba","ca","d");

        list.stream().
                filter(x->x.contains("a")).
                map(x->x+"sd").
                collect(Collectors.toList()).
                forEach(System.out::println);

        String[] words = new String[]{"Hello","World"};

        Arrays.stream(words).
                map(word->word.split("")).distinct().
                collect(Collectors.toList()).forEach(strs->{
                   for (String str:strs){
                       System.out.print(str+" ");
                   }
            System.out.println();
        });

        Arrays.stream(words).
                map(word->word.split("")).
                flatMap(word-> Arrays.stream(word)).
                distinct().
                collect(Collectors.toList()).forEach(System.out::print);

        System.out.println();
        Stream.of(Arrays.asList(1,2,3),Arrays.asList(4,5,6))
                .flatMap(nums->nums.stream()).collect(Collectors.toList()).
                forEach(System.out::print);

    // 将集合中的字符串中单词提取出来，不考虑特殊字符
        List<String> words1 = Arrays.asList("hello c++", "hello java", "hello python");
        List<String> result = words1.stream()
                // 将单词按照空格切合，返回Stream<String[]>类型的数据
                .map(word -> word.split(" "))
                // 将Stream<String[]>转换为Stream<String>
                .flatMap(Arrays::stream)
                // 去重
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result);

    }

}
